package com.mk.service.impl;

import com.mk.base.exception.ApplicationException;
import com.mk.base.exception.StatusCode;
import com.mk.base.service.impl.BaseService;
import com.mk.common.BeanCopier;
import com.mk.common.Config;
import com.mk.mapper.OrgMapper;
import com.mk.model.entity.Org;
import com.mk.model.request.OrgRequest;
import com.mk.service.IOrgService;
import com.mk.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 11:36 AM 3/14/18
 */
@Service
public class OrgService extends BaseService<OrgMapper,Org> implements IOrgService {

    @Autowired
    private IUserService userService;

    @Override
    @Transactional
    public Org save(OrgRequest request) {
        if(request == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请传入添加的组织信息");
        if(StringUtils.isEmpty(request.getTitle()) || request.getParent() == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"添加组织时请传入名称以及父节点");
        Org org = BeanCopier.copy(request,Org.class);
        Date nowDate = new Date();
        org.setCreateTime(nowDate);
        org.setUpdateTime(nowDate);
        if(super.insert(org))
            return org;
        else
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"添加组织信息失败");
    }

    @Override
    @Transactional
    public Boolean update(OrgRequest request) {
        if(request == null || request.getId() == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择编辑的组织信息");
        Org exist = get(request.getId());
        if(exist == null)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"编辑的组织信息不存在");
        exist = BeanCopier.copy(request,Org.class);
        exist.setUpdateTime(new Date());
        return super.updateById(exist);
    }

    @Override
    @Transactional
    public Boolean delete(Long id) {
        if(id == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择删除的组织信息");
        Org exist = get(id);
        if(exist == null)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"删除的组织信息不存在");
        if(!userService.orgCanDel(id))
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"组织信息正在使用，无法删除");
        exist.setUpdateTime(new Date());
        exist.setIsDeleted(Config.ABLE_CONFIG.UNABLE);
        return super.updateById(exist);
    }

    @Override
    public Org get(Long id) {
        return super.selectById(id);
    }

    @Override
    public List<Org> getAll(OrgRequest request) {
        return baseMapper.findAll(request, Config.ABLE_CONFIG.ABLE);
    }

    @Override
    public Boolean deleteChilds(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids))
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择需要删除的组织信息");
        StringBuffer sb = new StringBuffer();
        List<Org> orgs = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            Org org = get(ids.get(i));
            if(!userService.orgCanDel(ids.get(i)))
                sb.append(org.getTitle() + ",");
            org.setIsDeleted(Config.ABLE_CONFIG.UNABLE);
            org.setUpdateTime(new Date());
            orgs.add(org);
        }
        if(sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
            throw new ApplicationException(StatusCode.CONFLICT.getCode(), sb.append("正在使用，无法删除").toString());
        }
        return super.updateBatchById(orgs);
    }
}
